const jwt = require('jsonwebtoken');
const JWT_SECRET = 'your_jwt_secret';

module.exports = (req, res, next) => {
  const accessToken = req.headers['authorization']?.replace('Bearer ', '');
  if (!accessToken) return res.status(401).json({ message: '未登录或accessToken缺失' });
  try {
    const decoded = jwt.verify(accessToken, JWT_SECRET);
    req.user = decoded;
    next();
  } catch (err) {
    return res.status(401).json({ message: 'accessToken无效或已过期' });
  }
};